package com.ziyueyuwen.xiniao.controller.user.userrole;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ZyRole;
import com.ziyueyuwen.xiniao.bean.ZyUser;
import com.ziyueyuwen.xiniao.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;

/**
 * @author wancheng  on 2018/12/23.
 */
@Controller
@RequestMapping("/user/role")
public class UserRoleController {

    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/get_user_role.do")
    public void getUserRole(HttpServletRequest request,
                            HttpServletResponse response,
                            String userAccount) {
        HttpResContent<List<ZyRole>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取用户的身份信息");
        try {

            List<ZyRole> roles = roleService.getUerRolesByAccount(userAccount);
            saveSuccessMsgAndData(hrc, roles);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc);
        }
    }

    @RequestMapping(value = "/update_user_role.do")
    public void updateUserRole(HttpServletRequest request,
                               HttpServletResponse response,
                               String userAccount,
                               Integer[] roleIds) {
        HttpResContent<List<ZyRole>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改用户的身份信息");
        try {
            List<ZyRole> roles = roleService.updateUserRoles(userAccount, Arrays.asList(roleIds));
            saveSuccessMsgAndData(hrc, roles);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc);
        }
    }
}
